<?php
/**
 * CreateTime: 2023/12/20 15:05
 * User:LinQ
 */

namespace App\Jobs;

use App\Func\Office\WordFunc;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class LetterFileJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $tempPath; // 使用的模板的路径
    protected $tempData; // 使用的模板的数据
    protected $wordFunc; // 基础方法
    protected $wordFileName; // 模板转换后生成的word文件名,pdf也使用这个名词
    protected $wordOutPutPath; // 生成的 word的地址
    protected $wordDockerOutPutPath; // 对应容器中的地址
    protected $pdfOutPutPath; // pdf 输出的地址
    protected $waterImagePath; // 使用的水印的地址 【这里最好为docker中的地址】
    protected $imageOutPutPath; // 图片输出的地址
    protected $imageDockerOutPutPath; // 图片在容器中的地址
    protected $type; // 用来选择使用哪一个容器， 容器命名规则为 u_c_?

    public function __construct(
        $tempPath,
        $tempData,
        $wordFileName,
        $wordOutPutPath,
        $pdfOutPutPath,
        $waterImagePath,
        $imageOutPutPath,
        $type
    )
    {
        $this->tempPath = $tempPath;
        $this->tempData = $tempData;
        $this->wordFileName = $wordFileName;
        $this->wordOutPutPath =  Storage::path($wordOutPutPath);
        $this->wordDockerOutPutPath = $wordOutPutPath;
        $this->pdfOutPutPath = $pdfOutPutPath;
        $this->waterImagePath = $waterImagePath;
        $this->imageOutPutPath = Storage::path($imageOutPutPath);
        $this->imageDockerOutPutPath = $imageOutPutPath;
        $this->type = $type;
        $this->wordFunc = new WordFunc();
        // 自动创建文件夹
        if (!Storage::exists($this->wordOutPutPath)) {
            Storage::makeDirectory($this->wordOutPutPath);
        }
        if (!Storage::exists($pdfOutPutPath)) {
            Storage::makeDirectory($pdfOutPutPath);
        }
        if (!Storage::exists($imageOutPutPath)) {
            Storage::makeDirectory($imageOutPutPath);
        }

    }

    public function handle()
    {
        // 任务逻辑
        echo "任务开始".$this->wordDockerOutPutPath."\n";
        sleep(3);
        return 1;
//        $this->step1();

    }

    private function step1()
    {
        echo "步骤1\n";

        $wordPath = $this->wordFunc->TemplateProcessorCustom(
            $this->tempPath,
            $this->tempData,
            $this->wordOutPutPath.DIRECTORY_SEPARATOR.$this->wordFileName.".docx"
        );
        // 文件校验检查
        $i = 0;
        sleep(1);
        clearstatcache();
        while(!file_exists($wordPath) && $i<=5){
            clearstatcache();
            echo "IO阻塞1\n";
            $i++;
            usleep(500000);
        }

        // 检查文件是否成功验证
        if (file_exists($wordPath)) {
            $this->step2($wordPath);
        }
    }

    private function step2($wordPath)
    {
        echo "步骤2$wordPath\n";
        $pdfPath = $this->wordFunc->wordConvertPdf(
            $this->wordDockerOutPutPath.DIRECTORY_SEPARATOR.$this->wordFileName.".docx",
            $this->pdfOutPutPath.DIRECTORY_SEPARATOR.$this->wordFileName.".pdf",
            $this->type
        );
        $pdfPathFmt = json_decode($pdfPath,true);
        if($pdfPathFmt['status']){
            $this->step3(
                $pdfPathFmt['data']['savePath']
            );
        }

    }

    private function step3($pdfPath)
    {
        echo "步骤3$pdfPath\n";
        sleep(1);
        clearstatcache();
        // 文件校验检查
        $localPdfPath = Storage::path($pdfPath);
        $i = 0;
        while(!file_exists($localPdfPath) && $i<=5){
            clearstatcache();
            echo "IO阻塞3\n";
            $i++;
            usleep(500000);
        }
        if(file_exists($localPdfPath)){
            $this->step4(
                $pdfPath
            );
        }
    }

    private function step4($pdfPath){
        echo "步骤4$pdfPath\n";
        // 检查文件是否成功验证
        // 生成随机的图片文件夹
        $image = $this->wordFunc->pdfCoverToJpgNotLevel(
            Storage::path($pdfPath),
            $pdfPath,
            $this->imageOutPutPath,
            $this->imageDockerOutPutPath,
            $this->type
        );
        $imageFmt = json_decode($image,true);
        echo "步骤5\n";
        if($imageFmt['status']){
            return $imageFmt['data']['savePath'];
        }


    }
}